﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace OuterParser.OsvitaComUa
{
    public class OsvitaComUaParserInterface
    {
        public static void AddUnivs()
        {
            OuterParser.OsvitaUa.Notification.AddErrorToErrorsLog("osvita.com.ua start parseng");
            ParserOsvitaComUa parser = new ParserOsvitaComUa();

            parser.GetAllInfoAboutUniv(@"http://www.osvita.com.ua/ua/universities/47/");

            parser.GetDirectionInPage(@"http://www.osvita.com.ua/ua/universities/");
            parser.GetAllUnivHref();
            Console.WriteLine();
        }
    }
    class ParserOsvitaComUa
    {
        public string mainHref = @"http://www.osvita.com.ua";

        /// <summary>
        /// посилання на сторінки з університетами за напрямками підготовки
        /// </summary>
        public List<string> dirsHref = new List<string>();
        List<string> univHref = new List<string>();
        int countOfUnivs = 0;
        public void GetDirectionInPage(string href)
        {
            int i = 0;
            do
            {
                try
                {
                    WebRequest firstRequest = WebRequest.Create(href);

                    using (var response = firstRequest.GetResponse())
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(stream, Encoding.GetEncoding(65001)))//utf-8
                            {
                                string allText = reader.ReadToEnd();
                                string forLength = "<h3>Пошук ВНЗ за напрямками</h3>";
                                allText = allText.Remove(0, allText.IndexOf(forLength) + forLength.Length);
                                allText = allText.Remove(allText.IndexOf("<h3>Пошук ВНЗ по містах</h3>"));
                                allText = allText.Remove(allText.IndexOf("<div"));
                                allText = allText.Replace("<ul class=\"direct2\" style=\"float:left\">", "").Replace("</ul>", "");

                                int index = 0;
                                string linkStr = "";
                                do
                                {
                                    index = allText.IndexOf("</a>");
                                    if (index == -1) break;
                                    linkStr = allText.Remove(index);
                                    allText = allText.Remove(0, index + 4);

                                    linkStr = linkStr.Remove(0, linkStr.IndexOf("\"") + 1);
                                    linkStr = linkStr.Remove(linkStr.IndexOf("\""));
                                    dirsHref.Add(linkStr);
                                }
                                while (true);
                                OuterParser.OsvitaUa.Notification.AddErrorToErrorsLog("direction links add");
                                return;

                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    OuterParser.OsvitaUa.Notification.AddErrorToErrorsLog(string.Format("error parse direction href={0} \n{1}", href, e.ToString()));
                    if (i > 10) i++;
                    else break;
                }
            } while (true);

        }
        public void GetAllUnivHref()
        {
            foreach (string s in dirsHref)
            {
                GetHrefToUniversityInPage(mainHref + s);
            }
            OuterParser.OsvitaUa.Notification.AddErrorToErrorsLog("add all univ href");

            foreach (string st in univHref)
            {
                GetAllInfoAboutUniv(mainHref + st);
            }
        }
        public void GetHrefToUniversityInPage(string href)
        {
            int i = 0;
            string nextPage = "";
            do
            {
                try
                {
                    WebRequest firstRequest = WebRequest.Create(href);

                    using (var response = firstRequest.GetResponse())
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(stream, Encoding.GetEncoding(65001)))//utf-8
                            {
                                string allText = reader.ReadToEnd();
                                allText = allText.Remove(0, allText.IndexOf("paddContent"));
                                allText = allText.Remove(0, allText.IndexOf("</form>"));
                                allText = allText.Remove(0, allText.IndexOf("<tbody>") + 7);
                                allText = allText.Remove(0, allText.IndexOf("f_bor"));

                                //next page
                                string addstr = "наступна";
                                if (allText.Contains(addstr))
                                {
                                    nextPage = allText.Remove(allText.IndexOf(addstr));
                                    if (nextPage.Contains("попередня")) nextPage = nextPage.Remove(0, nextPage.IndexOf("попередня"));
                                    nextPage = nextPage.Remove(0, nextPage.LastIndexOf("<a") + 2);
                                    if (nextPage.Contains("href"))
                                    {
                                        nextPage = nextPage.Remove(0, nextPage.IndexOf("href"));
                                        nextPage = nextPage.Remove(0, nextPage.IndexOf("\"") + 1);
                                        nextPage = nextPage.Remove(nextPage.IndexOf("\""));
                                    }
                                    else
                                    {
                                        nextPage = "";
                                    }
                                }
                                allText = allText.Remove(allText.IndexOf("</table>"));

                                int index = 0;
                                do
                                {
                                    index = allText.IndexOf("</tr>");
                                    if (index == -1) break;
                                    addstr = allText.Remove(index);
                                    allText = allText.Remove(0, index + 5);

                                    addstr = addstr.Remove(0, addstr.IndexOf("href=\"") + 6);
                                    addstr = addstr.Remove(addstr.IndexOf("\""));
                                    if (!univHref.Contains(addstr))
                                    {
                                        univHref.Add(addstr);
                                        countOfUnivs++;
                                        Console.WriteLine(countOfUnivs);
                                    }
                                }
                                while (true);
                            }
                        }
                    }
                    if (nextPage != "")
                    {
                        GetHrefToUniversityInPage(mainHref + nextPage);
                    }
                    return;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    OuterParser.OsvitaUa.Notification.AddErrorToErrorsLog(string.Format("error parse all university pages href={0} \n{1}", href, e.ToString()));
                    if (i > 10) i++;
                    else break;
                }
            } while (true);
        }
        public void GetAllInfoAboutUniv(string href)
        {
            string allText = GetHTMLByHref(href);
            //name
            string name = allText.Remove(0, allText.IndexOf("<h1 itemprp=\"name\""));
            name = name.Remove(0, name.IndexOf(">") + 1);
            name = name.Remove(name.IndexOf("<"));
            //short name ?????
            string nameShort = "";
            string[] nStr = name.Split(' ');
            if (name.Contains("("))
            {
                nameShort = name.Remove(name.IndexOf(")"));
                nameShort = nameShort.Remove(0, nameShort.IndexOf("(") + 1);
                name = name.Remove(name.IndexOf("("));
            }
            else
            {
                if (nStr[nStr.Length - 1].Length < 10)
                {
                    nameShort = nStr[nStr.Length - 1];
                    name = name.Replace(nameShort, "");
                }
            }



            Models.OsvitaComUa_University univ = new Models.OsvitaComUa_University();

            univ.Name = name;
            univ.ShortName = nameShort;

            //table
            if (allText.Contains("студентів"))
            {
                string table = allText.Remove(0, allText.IndexOf("студентів"));
                table = table.Remove(table.IndexOf("</table>"));
                string str = "";
                string val = "";
                int index = 0;
                do
                {
                    index = table.IndexOf("</tr>");
                    if (index == -1) break;
                    str = table.Remove(index);
                    table = table.Remove(0, index + 5);

                    val = str.Remove(str.LastIndexOf("</td>"));
                    val = val.Remove(0, val.LastIndexOf(">") + 1);

                    if (str.Contains("Студентів")) univ.CountOfStydent = int.Parse(val);
                    if (str.Contains("Викладачів")) univ.CountOfTeacher = int.Parse(val);
                    if (str.Contains("Кандидатів наук")) univ.CountOfCandidatesOfSciences = int.Parse(val);
                    if (str.Contains("Професорів і докторів наук")) univ.CountOfProfessorwsAndDoctors = int.Parse(val);
                }
                while (true);
            }
            //Abit info
            if (allText.Contains("<a name=\"entrant"))
            {
                univ.ForAbiturientsHTML = allText.Remove(0, allText.IndexOf("entrant"));
                univ.ForAbiturientsHTML = univ.ForAbiturientsHTML.Remove(0, univ.ForAbiturientsHTML.IndexOf("</div>") + 6);
                if (univ.ForAbiturientsHTML.Contains("<a name=\"about\""))
                { 
                    univ.ForAbiturientsHTML = univ.ForAbiturientsHTML.Remove(univ.ForAbiturientsHTML.IndexOf("<a name=\"about\"")); 
                }
                else
                {
                    univ.ForAbiturientsHTML = univ.ForAbiturientsHTML.Remove(univ.ForAbiturientsHTML.IndexOf("<a"));
                }
            }
            //Detail info
            if (allText.Contains("<a name=\"about\""))
            {
                univ.DetailInfoAboutUniv = allText.Remove(0, allText.IndexOf("<a name=\"about\""));
                univ.DetailInfoAboutUniv = univ.DetailInfoAboutUniv.Remove(0, univ.DetailInfoAboutUniv.IndexOf("</div>"));
                if (univ.DetailInfoAboutUniv.Contains("<a name=\"gallery\""))
                {
                    univ.DetailInfoAboutUniv = univ.DetailInfoAboutUniv.Remove(univ.DetailInfoAboutUniv.IndexOf("<a name=\"gallery\""));
                }
                else
                {
                    univ.DetailInfoAboutUniv = univ.DetailInfoAboutUniv.Remove(univ.DetailInfoAboutUniv.IndexOf("<a"));
                }
            }

            //add univ to dataBase
            OsvitaComUaServices.AddUniv(univ);

            //galery


        }
        public string GetHTMLByHref(string href)
        {
            int i = 0;
            do
            {
                try
                {
                    WebRequest firstRequest = WebRequest.Create(href);

                    using (var response = firstRequest.GetResponse())
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            using (var reader = new StreamReader(stream, Encoding.GetEncoding(65001)))//utf-8
                            {
                                return reader.ReadToEnd();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    OuterParser.OsvitaUa.Notification.AddErrorToErrorsLog(string.Format("error parse direction href={0} \n{1}", href, e.ToString()));
                    if (i > 10) i++;
                    else break;
                }
            } while (true);
            return null;
        }

    }
    class OsvitaComUaServices
    {
      static  Models.PulsatrixStagingDbEntities db = new Models.PulsatrixStagingDbEntities();

      public static int GetUnivIdByName(string name)
      {
          var a = db.OsvitaComUa_University.FirstOrDefault(x=>x.Name==name);
          if (a == null) return -1;
          return a.Id;
      }
      static  public void AddUniv(Models.OsvitaComUa_University univ)
        {
            try
            {
                if (GetUnivIdByName(univ.Name) != -1) return;
                db.OsvitaComUa_University.Add(univ);
            }
            catch (Exception e)
            {
                OuterParser.OsvitaUa.Notification.AddErrorToErrorsLog(string.Format("univ add error \n{0}",e.ToString()));
            }
        }

    }
}
